const { app, BrowserWindow, ipcMain, globalShortcut } = require("electron");
const path = require("path");
const isDev = require("electron-is-dev");
let mainWindow;
function createWindow() {
  mainWindow = new BrowserWindow({
    width: 300,
    height: 200,
    icon: path.join(__dirname, "favicon.ico"), // 设置窗口图标
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    },
    frame: false,
    transparent: true, // 透明背景
    alwaysOnTop: true, // 置顶
    resizable: false, // 不可调整大小
    skipTaskbar: true, // 不在任务栏显示
  });
  mainWindow.loadURL(
    isDev
      ? "http://localhost:3000"
      : `file://${path.join(__dirname, "../build/index.html")}`
  );
  // 开发者工具
  if (isDev) {
    mainWindow.webContents.openDevTools({ mode: "detach" });
  }
  mainWindow.on("closed", () => (mainWindow = null));
  globalShortcut.register("Control+Space", () => {
    if (!mainWindow) return;
    if (mainWindow.isVisible()) {
      mainWindow.hide();
    } else {
      mainWindow.show();
    }
  });
}
app.whenReady().then(createWindow);
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
app.on("activate", () => {
  if (mainWindow === null) createWindow();
});
// 处理穿透消息
ipcMain.on("set-ignore-mouse-events", (event, ignore) => {
  mainWindow.setIgnoreMouseEvents(ignore, { forward: true });
});
ipcMain.on("resize-window", (event, { width, height }) => {
  if (mainWindow) {
    mainWindow.setSize(width, height, true);
  }
});
